package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.AdChannelDTO;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/7/29
 */
@RestController
@RequestMapping("/api/v1/channel")
@Slf4j
@Api(value = "频道管理",tags = "channel",description = "频道管理api")
public class AdChannelController {

    @Autowired
    private AdChannelService adChannelService;

    @ApiOperation("频道列表分页查询")
    @PostMapping("/list")
    public ResponseResult getAdChannelListBuPage(@RequestBody AdChannelDTO channelDTO) {

        log.info("query page by {}" + channelDTO);

        // 参数判断
        if (channelDTO == null) {
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDTO.checkParam();// 检查分页参数 如果为空 设置默认值

        return adChannelService.queryPage(channelDTO);
    }

    @ApiOperation("新增频道")
    @PostMapping("/save")
    public ResponseResult saveAdChannel(@RequestBody AdChannel adChannel){

        log.info("save AdChannel {}" + adChannel);

        if (Objects.isNull(adChannel)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        return adChannelService.saveAdChannel(adChannel);
    }

    @ApiOperation("修改频道信息")
    @PostMapping("/update")
    public ResponseResult updateAdChannel(@RequestBody AdChannel adChannel){

        log.info("update AdChannel with {}" + adChannel);

        if (Objects.isNull(adChannel)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        return adChannelService.updateAdChannel(adChannel);
    }

    @ApiOperation("根据id删除频道")
    @GetMapping("/del/{id}")
    public ResponseResult deleteAdChannelById(@PathVariable Integer id){

        log.info("delete channel by id {} " + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        return adChannelService.deleteAdChannelById(id);
    }

    @ApiOperation("频道列表查询")
    @GetMapping("/channels")
    public ResponseResult getChannelList(){
        return ResponseResult.okResult(adChannelService.list());
    }
}
